<?php
namespace app\common\model;
class BuildingPoint extends \think\Model
{
    protected $resultSetType = 'collection';
    //相应楼盘
    public function point(){
        return $this->hasOne('Building','building_id','id');
    }
    //添加点位信息
    public function inserts(){
        $data = input('param.');
        switch ($data['name']){
            case 1 :$map[] = ['building_id','eq',$data['building_id']];
                $map[] = ['role','neq', 0];
                $result = $this -> where($map) -> order('role ASC') -> select();
                if($result)
                    $result =$result ->toArray();
                if(!empty($result)){
                    $datainfo = [
                        ['id' => $result[0]['id'],'building_id' => intval($data['building_id']),
                            'role' => 5,'action' => 0,
                            'point' => $data['sell'].','.($data['sellUnit'] == 0?'%':'元')],
                        ['id' => $result[1]['id'],'building_id' => intval($data['building_id']),
                            'role' => 3,'action' => 0,
                            'point' => $data['xm'].','.($data['xmUnit'] == 0?'%':'元')],
                        ['id' => $result[2]['id'],'building_id' => intval($data['building_id']),
                            'role' => 7,'action' => 0,
                            'point' => $data['counselor'].','.($data['counselorUnit'] == 0?'%':'元')],
                        ['id' => $result[3]['id'],'building_id' => intval($data['building_id']),
                            'role' => 8,'action' => 0,
                            'point' => $data['section'].','.($data['sectionUnit'] == 0?'%':'元')],
                        ['id' => $result[4]['id'],'building_id' => intval($data['building_id']),
                            'role' => 9,'action' => 0,
                            'point' => $data['outField'].','.($data['outFieldUnit'] == 0?'%':'元')],
                        ['id' => $result[5]['id'],'building_id' => intval($data['building_id']),
                            'role' => 14,'action' => 0,
                            'point' => $data['channel'].','.($data['channelUnit'] == 0?'%':'元')],
                        ['id' => $result[6]['id'],'building_id' => intval($data['building_id']),
                            'role' => 17,'action' => 0,
                            'point' => $data['extend'].','.($data['extendUnit'] == 0?'%':'元')],
                        ['id' => $result[7]['id'],'building_id' => intval($data['building_id']),
                            'role' => 18,'action' => 0,
                            'point' => $data['generalize']
                                .','.($data['generalizeUnit'] == 0?'%':'元')],
                        ['id' => $result[8]['id'],'building_id' => intval($data['building_id']),
                            'role' => 31,'action' => 0,
                            'point' => $data['service'].','.($data['serviceUnit'] == 0?'%':'元')],
                        ['id' => $result[9]['id'],'building_id' => intval($data['building_id']),
                            'role' => 32,'action' => 0,
                            'point' => $data['serviceGeneralize']
                                .','.($data['serviceGeneralizeUnit'] == 0?'%':'元')],
                    ];
                }else{
                    $datainfo = [
                        ['building_id' => intval($data['building_id']),'role' => 5,'action' => 0,
                            'point' => $data['sell'].','.($data['sellUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']),'role' => 3,'action' => 0,
                            'point' => $data['xm'].','.($data['xmUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 7,'action' => 0,
                            'point' => $data['counselor'].','.($data['counselorUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 8,'action' => 0,
                            'point' => $data['section'].','.($data['sectionUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 9,'action' => 0,
                            'point' => $data['outField'].','.($data['outFieldUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 14,'action' => 0,
                            'point' => $data['channel'].','.($data['channelUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 17,'action' => 0,
                            'point' => $data['extend'].','.($data['extendUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 18,'action' => 0,
                            'point' => $data['generalize']
                                .','.($data['generalizeUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 31,'action' => 0,
                            'point' => $data['service'].','.($data['serviceUnit'] == 0?'%':'元')],
                        ['building_id' => intval($data['building_id']), 'role' => 32,'action' => 0,
                            'point' => $data['serviceGeneralize']
                                .','.($data['serviceGeneralizeUnit'] == 0?'%':'元')],
                    ];
                }break;
            case 2 :$map[] = ['building_id','eq',$data['building_id']];
                    $map[] = ['action','in', array(1,2)];
                    $result = $this -> where($map) -> select();
                if($result){
                    $datainfo = [
                        ['id' => $result[0]['id'],'building_id'=>intval($data['building_id']),
                            'role' => 0,'action' => 1,
                            'point' => $data['one'].','.($data['oneUnit'] == 0?'%':'元')],
                        ['id' => $result[1]['id'],'role' => 0,
                            'building_id'=>intval($data['building_id']),'action' => 2,
                            'point' => $data['two'].','.($data['twoUnit'] == 0?'%':'元')],
                    ];
                }else{
                    $datainfo = [
                        ['role' => 0,'building_id'=>intval($data['building_id']),'action' => 1,
                            'point' => $data['one'].','.($data['oneUnit'] == 0?'%':'元')],
                        ['role' => 0,'building_id'=>intval($data['building_id']),'action' => 2,
                            'point' => $data['two'].','.($data['twoUnit'] == 0?'%':'元')],
                    ];
                }break;
            case 3 :$map[] = ['building_id','eq',$data['building_id']];
                    $map[] = ['action','in', array(3,4)];
                    $result = $this -> where($map) -> select();
                    if($result){
                        $datainfo = [
                            ['id' => $result[0]['id'],'building_id'=>intval($data['building_id']),
                                'role' => '','action' => 3,
                                'point' => $data['top'].','.($data['topUnit'] == 0?'%':'元')],
                            ['id' => $result[1]['id'],'building_id'=>intval($data['building_id']),
                                'role' => '','action' => 4,
                                'point' => $data['bottom'].','.($data['bottomUnit'] == 0?'%':'元')],
                        ];
                    }else{
                        $datainfo = [
                            ['building_id'=>intval($data['building_id']),'role' => '','action' => 3,
                                'point' => $data['top'].','.($data['topUnit'] == 0?'%':'元')],
                            ['building_id'=>intval($data['building_id']),'role' => '','action' => 4,
                                'point' => $data['bottom'].','.($data['bottomUnit'] == 0?'%':'元')],
                        ];
                    }break;
            case 4 :$map[] = ['building_id','eq',$data['building_id']];
                    $map[] = ['action','eq', 5];
                    $result = $this -> where($map) -> find();
                    if($result){
                        $datainfo = [
                            ['id' => $result['id'],'role' => '',
                                'building_id'=> intval($data['building_id']),
                                'action' => 5,
                                'point' => $data['one'].','.($data['oneUnit'] == 0?'%':'元')],
                        ];
                    }else{
                        $datainfo = [
                            ['role' => '','action' => 5,'building_id'=> intval($data['building_id']),
                                'point' => $data['one'].','.($data['oneUnit'] == 0?'%':'元')],
                        ];
                    }break;default;
        }
        $this -> saveAll($datainfo);
        return json([
            'code' => 1,
            'message' => '更新成功'
        ]);
    }
}